CoAP Server : CoAP server object

更新时间:
2024-03-04
下载文档

CoAP Server : CoAP server object

The coap module provides basic coap protocol support and includes the following features:

  • CoapServer mode.
  • CoapClient mode.

This document focuses on the use of CoapServer.

User can use the following code to import the coap module and access CoapServer .

var coap = require('coap');

Support

The following shows CoapServer module APIs available for each permissions.

 User ModePrivilege Mode
coap.createServer
coap.mode
server.start
server.stop
response.send
response.end

CoapServer Class

The CoapServer inherit from UdpServer. It can be create by coap.createServer.

coap.createServer(saddr[, opts[, dtlsOpt]])

  • saddr {Object} Server udp socket address.
  • opts {Object} Server options. Details:
    • ioTimeout {Integer} Udp io timeout. default: 0.
    • retryTimeout {Integer} The first retry timeout. The comfirm message will be retried while it's not acknowledged in retryTimeout times. default: 10s.
    • retryTimes {Integer} The retry counts. The comfirm message will be retried retryTimes times until it is acknowledged . default: 3.
    • periodTimeout {Integer} The response timeout. If the server not response in periodTimeouttimes, the request will timeout. default: 60.
  • dtlsOpt {Object} DTLS securely connections options. default: undefined, means use UDP connection.

This method creates a CoapServer.

Example

  • Create server:
var Udp = require('udp');
var coap = require('coap');

var saddr = Udp.sockaddr(Udp.INADDR_ANY, 5683);
var server = coap.createServer(saddr);

coap.mode()

  • Returns: {String} Coap work mode.

Get the current process CoAP work mode.

  • on CoAP is enabled.
  • off CoAP is not enabled.

CoapServer Object

server.start()

Start coap server.

server.stop()

Stop coap server.

CoapServer Events

start

Emitted when the coap server start done.

stop

Emitted when the coap server stop.

request

Emitted when a coap request comes to server. It has the following properties:

  • req {CoapSerRequest} The coap request object, see CoapSerRequest.
  • res {CoapSerResponse} The http response object, see CoapSerResponse.

CoapSerRequest Object

CoapSerRequest the same as Package class, see the CoapPackage module for more details. User can access reuqest message from request object.

CoapSerResponse Object

CoapSerResponse inherit from Package class, see the CoapPackage module for details.

response.send(chunk[, opts])

  • chunk {String | Object | Buffer} Coap payload data.
  • opts {Object} send options, details:
    • confirm {Boolean} The response send is confirm or no.
    • code {String} The response status. See CoapPackage.code.
    • options {Object} The response coap options. See CoapPackage.options.

Send data to client. In normal mode, this interface is the same as response.end, used to respond to client requests and complete the response. In observe mode, the user can use this interface to notify the client until the response is ended with response.end or reset by the client or server.

response.end([chunk])

  • chunk {String | Object | Buffer} Coap payload data.

Send data to client and end response.

CoapSerResponse Events

error

A error event is emitted while responsed to client's request. Event callback argument:

  • e {Error} Error object.

finish

A finish event is emitted when responsed to client's success. If errorevent is emitted, this event will not emitted.

Example

Base server

var Udp = require('udp');
var coap = require('coap');
var iosched = require('iosched');

var saddr = Udp.sockaddr(Udp.INADDR_ANY, 5683);
var server = coap.createServer(saddr);

server.on('request', function (req, res) {
  console.log('On request.');
  if (req.payload) {
    console.log('Recv message:', req.payload.toString());
  }

  res.end('Hello, world.');
});

server.start();

while (true) {
  iosched.poll();
}

Observe mode

var Udp = require('udp');
var iosched = require('iosched');
var CoapServer = require('coap').CoapServer;

var saddr = Udp.sockaddr(Udp.INADDR_ANY, 5683);
var server = CoapServer.create(saddr);

server.on('request', function (req, res) {
  console.log('Server on request.');

  var timer = undefined;

  res.on('error', function (e) {
    console.info('Server response error:', e);
    if (timer) {
      clearInterval(timer);
    }
  })

  res.on('finish', function () {
    console.info('Server response finish.');
    if (timer) {
      clearInterval(timer);
    }
  });

  if (req.isObserve()) {
    timer = setInterval(function () {
      console.log('Send data.');
      res.send({ temperature: 35 });
    }, 1000, res);

  } else {
    console.log('End data.');
    res.end({ temperature: 35 });
  }
});

server.start();

while (true) {
  iosched.poll();
}
文档内容是否对您有所帮助?
有帮助
没帮助